package com.wslNB.eureka.controller;


import com.wslNB.eureka.domain.GodWSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    //根据商品id调用商品信息
    @GetMapping("/findGogWSLById/{id}")
    public GodWSL findGogWSLById(@PathVariable Integer id){
        System.out.println("findGogWSLById="+id);

       /* //1.调用provider服务中的接口
        String url = "http://localhost:8081/provider/findOne/"+id;
*/

        //获取实例对象
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER");
        ServiceInstance serviceInstance = instances.get(0);

        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        String instanceId = serviceInstance.getInstanceId();

        String url = "http://"+host+":"+port+"/provider/findOne/"+instanceId;

        GodWSL godWSL = restTemplate.getForObject(url, GodWSL.class);
        return godWSL;

    }



}
